package com.example;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Jason
 * @PACKAGE_NAME com.chatroomspringboot
 * @Description Lambda表达式
 * @date 2023-11-24 12:29
 */
interface MyInterface{
    public void doSomething(String s);
}
public class Lambda {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("apple", "banana", "orange");
        /*遍历循环*/
        /*for (String s : list){
            System.out.println(s);
        }
        System.out.println("------------------------");
        list.forEach(System.out::println);
        System.out.println("------------------------");
        list.forEach(s -> System.out.println(s));
        System.out.println("------------------------");
        list.forEach(s -> {
            System.out.println(s);
        });*/

        /*排序*/
        /*Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        System.out.println("------------------------");
        Collections.sort(list, (o1, o2) -> o1.compareTo(o2));

        System.out.println("------------------------");
        Collections.sort(list, (o1, o2) -> {
            return o1.compareTo(o2);
        });*/

        /*过滤*/
        /*ArrayList<String> list2 = new ArrayList<>();
        for (String s : list) {
            if(s.startsWith("a")){
                list2.add(s);
            }
        }
        List<String> list3 = list.stream().filter(s -> s.startsWith("a")).collect(Collectors.toList());
        */

        /*映射*/
        /*List<Integer> list2 = new ArrayList<>();
        for(String s : list){
            list2.add(s.length());
        }

        List<Integer> list3 = list.stream().map(s -> s.length()).collect(Collectors.toList()); //这里map是对管道流中的数据做转换处理
        List<Integer> list4 = list.stream().map(s -> {
            return s.length();
        }).collect(Collectors.toList());*/

        /*规约*/
        /*List<Integer> list = Arrays.asList(1,2,3,4,5);
        int sum = 0;
        for(Integer v : list){
            sum += v;
        }
        System.out.println(sum);

        int sum2 = list.stream().reduce(0,(a,b) -> a+b); //初始值为0
        int sum3 = list.stream().reduce(0,(a,b) -> {
            return a+b;
        });*/

        /*分组*/
        /*Map<Integer,List<String>> groups = new HashMap<>();
        for(String s : list){
            int length = s.length();
            if(!groups.containsKey(length)){
                groups.put(length,new ArrayList<>());
            }
            groups.get(length).add(s);
        }
        System.out.println(groups);

        Map<Integer,List<String>> groups2 = list.stream().collect(Collectors.groupingBy(String::length));
        Map<Integer,List<String>> groups3 = list.stream().collect(Collectors.groupingBy(s -> s.length()));
        System.out.println(groups2);*/

        /*接口实现*/
       /*MyInterface myInterface = new MyInterface(){
            @Override
            public void doSomething(String s) {
                System.out.println(s);
            }
        };
       myInterface.doSomething("hello world");

       MyInterface myInterface1 = (s) -> System.out.println(s);
       myInterface1.doSomething("你好");*/

        /*创建线程*/
        /*Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程");
            }
        });
        thread.start();

        Thread thread1 = new Thread(() -> System.out.println("线程Lam"));
        thread1.start();*/

        /*Optional避免空指针*/
        /*String str = "helloworld";
        if (str != null) {
            System.out.println(str.toUpperCase());
        }

        Optional.ofNullable(str).map(String::toUpperCase).ifPresent(System.out::println);*/

        /*流水线操作*/
        List<String> list1 = new ArrayList<>();
        for(String s : list){
            if(s.startsWith("a")){
                list1.add(s.toUpperCase());
            }
        }
        Collections.sort(list1);

        List<String> list2 = list.stream().filter(s -> s.startsWith("a")).map(String::toUpperCase).sorted().collect(Collectors.toList());
        System.out.println(list1);
        System.out.println(list2);

    }
}
